package com.lottery.im.module.tim.message;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.module.chat.adapter.ChatAdapter;
import com.lottery.im.module.tim.utils.TimFileUtil;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMFileElem;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMMessage;

/**
 * 文件消息
 */
public class FileMessage extends Message {

  public FileMessage(TIMMessage message) {
    this.message = message;
  }

  public FileMessage(String filePath) {
    message = new TIMMessage();
    TIMFileElem elem = new TIMFileElem();
    elem.setPath(filePath);
    elem.setFileName(filePath.substring(filePath.lastIndexOf("/") + 1));
    message.addElement(elem);
  }

  /**
   * 显示消息
   *
   * @param viewHolder 界面样式
   * @param context 显示消息的上下文
   */
  @Override
  public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {
    //    clearView(viewHolder);
    //    if (checkRevoke(viewHolder)) return;
    //    TIMFileElem e = (TIMFileElem) message.getElement(0);
    //    TextView tv = new TextView(MyApplication.getInstance());
    //    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
    //    tv.setTextColor(MyApplication.getInstance().getResources().getColor(isSelf() ? R.color.white : R.color.black));
    //    tv.setText(e.getFileName());
    //    getBubbleView(viewHolder).addView(tv);
    //    showStatus(viewHolder);
  }


  /**
   * 获取消息摘要
   */
  @Override
  public String getSummary() {
    String str = getRevokeSummary();
    if (str != null) return str;

    StringBuilder result = new StringBuilder();

    TIMGroupMemberInfo senderGroupMemberProfile = message.getSenderGroupMemberProfile();
    if (null != senderGroupMemberProfile) {
      // 说明这是群聊消息
      if (TextUtils.isEmpty(message.getSenderGroupMemberProfile().getNameCard())) {
        // 有自己的群名片
        result.append(message.getSenderProfile().getNickName() + "：");
      } else {
        // 无自己的群名片
        result.append(message.getSenderGroupMemberProfile().getNameCard() + "：");
      }
    }
    result.append(MyApplication.getInstance().getString(R.string.summary_file));
    return result.toString();
  }

  /**
   * 保存消息或消息文件
   */
  @Override
  public void save() {
    if (message == null) return;
    final TIMFileElem e = (TIMFileElem) message.getElement(0);
    String[] str = e.getFileName().split("/");
    String filename = str[str.length - 1];
    if (TimFileUtil.isFileExist(filename, Environment.DIRECTORY_DOWNLOADS)) {
      Toast.makeText(MyApplication.getInstance(),
          MyApplication.getInstance().getString(R.string.save_exist),
          Toast.LENGTH_SHORT).show();
      return;
    }

    e.getToFile(TimFileUtil.getCacheFilePath(filename), new TIMCallBack() {
      @Override
      public void onError(int i, String s) {
        Log.e(TAG, "getFile failed. code: " + i + " errmsg: " + s);
      }

      @Override
      public void onSuccess() {

      }
    });
  }
}
